<?php

namespace Kunfeng\Dify\Model\Completion;

use Kunfeng\Dify\Enum\ResponseMode;

/**
 * 文本生成请求模型
 */
class CompletionRequest
{
    private $inputs;
    private $responseMode;
    private $user;
    private $files;

    public function __construct(
        array $inputs,
        string $responseMode = ResponseMode::BLOCKING,
        string $user = '',
        ?array $files = null
    ) {
        $this->inputs = $inputs;
        $this->responseMode = $responseMode;
        $this->user = $user;
        $this->files = $files;
    }

    /**
     * 创建请求
     */
    public static function create(array $inputs, string $user): self
    {
        return new self($inputs, ResponseMode::BLOCKING, $user);
    }

    /**
     * 使用建造者模式
     */
    public static function builder(): CompletionRequestBuilder
    {
        return new CompletionRequestBuilder();
    }

    /**
     * 设置响应模式
     */
    public function withResponseMode(string $responseMode): self
    {
        return new self(
            $this->inputs,
            $responseMode,
            $this->user,
            $this->files
        );
    }

    /**
     * 设置文件
     */
    public function withFiles(array $files): self
    {
        return new self(
            $this->inputs,
            $this->responseMode,
            $this->user,
            $files
        );
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        $data = [
            'inputs' => $this->inputs,
            'response_mode' => $this->responseMode,
            'user' => $this->user,
        ];

        if ($this->files !== null) {
            $data['files'] = $this->files;
        }

        return $data;
    }

    public function getInputs(): array
    {
        return $this->inputs;
    }

    public function getResponseMode(): string
    {
        return $this->responseMode;
    }

    public function getUser(): string
    {
        return $this->user;
    }

    public function getFiles(): ?array
    {
        return $this->files;
    }
}

/**
 * 文本生成请求构建器
 */
class CompletionRequestBuilder
{
    private $inputs;
    private $responseMode = ResponseMode::BLOCKING;
    private $user = '';
    private $files;

    public function inputs(array $inputs): self
    {
        $this->inputs = $inputs;
        return $this;
    }

    public function responseMode(string $responseMode): self
    {
        $this->responseMode = $responseMode;
        return $this;
    }

    public function user(string $user): self
    {
        $this->user = $user;
        return $this;
    }

    public function files(array $files): self
    {
        $this->files = $files;
        return $this;
    }

    public function build(): CompletionRequest
    {
        if (!isset($this->inputs)) {
            throw new \InvalidArgumentException('inputs is required');
        }
        if (empty($this->user)) {
            throw new \InvalidArgumentException('user is required');
        }

        return new CompletionRequest(
            $this->inputs,
            $this->responseMode,
            $this->user,
            $this->files
        );
    }
}
